define(["jquery", "oms_store/account", "bbcare", "ac/app/applicationCache"], function ($, $store, $bb, $applicationCache) {
	var defaults, timer, login = {}

	//显示异常信息提醒
	var showMessage = function (msg, time) {
		time = time ? time : 5000
		$("#" + defaults.domId + " .bb-message").text(msg)
		timer = setTimeout(function () {
			$("#" + defaults.domId + " .bb-message").text("")
		}, time)
	}

	/**
	 * 请求登录成功后的回调函数
	 */
	var callbackSuccess = function (data, account) {
		data.password = data.remember ? data.password : ""
		$bb.setIocalStorage("data", JSON.stringify(data))
		if (typeof (Storage) !== "undefined") {
			// 若本地存在未上传的value，则上传；否则进入home页面
			var cacheSave = JSON.parse($bb.getIocalStorage("cacheSave"))
			if ($bb.getIocalStorage("cacheSave") && cacheSave.length > 0) {
				window.location.href = "uploadOffline.html"
			} else {
				if (account.apps.length == 1 && (!account.apps[0].auProjectRoleRel || account.apps[0].auProjectRoleRel.length == 1)) {
					account.apps[0].status = 1
					if (account.apps[0].auProjectRoleRel) {
						account.apps[0].auProjectRoleRel[0].status = "1"
						account.apps[0].auProjectRoleRel[0].selectStatus = 1
					}
					window.location.href = "home.html"
				} else {
					window.location.href = "projects.html"
				}
			}
			$bb.setSessionStorage("account", JSON.stringify(account))

		} else {
			showMessage("您的浏览器版本过低，请升级浏览器!")
		}
	}

	//登录回调
	var callback = function (success, account, data, code, msg) {
		$("#" + defaults.domId + " .bb-submit").html("登录").removeClass("disabled")
		if (success) {
			callbackSuccess(data, account)
		} else {
			showMessage(msg)
		}
	}

	// 离线登录回调里的提示
	var offlineLoginTip = function (obj, msg) {
		showMessage(msg)
		obj.html("登录").removeClass("disabled")
	}

	// 离线登录回调
	var offlineLogin = function (data, obj) {
		var oldData = JSON.parse($bb.getIocalStorage("data"))
		if (oldData && oldData.username && oldData.password) {
			if (oldData.username == data.username && oldData.password == data.password) {
				window.location.href = "cache.html"
			} else {
				offlineLoginTip(obj, "请输入正确的账号和密码！")
			}
		} else {
			offlineLoginTip(obj, "请先在在线状态下登录后再进入离线登录！")
		}
	}

	//登录提交事件
	var onSubmit = function () {
		var data = {}
		data.username = $("#" + defaults.domId + " .bb-username").val()
		data.password = $("#" + defaults.domId + " .bb-password").val()
		data.remember = $("#" + defaults.domId + " .bb-remember-me").is(':checked')

		// if (!data.username || data.username.trim().length != 11) {
		// 	showMessage("账号请输入11位手机号！")
		// 	return
		// }
		if (!data.username) {
			showMessage("账号请输入11位手机号或账号！")
			return
		}

		if (!data.password || data.password.trim().length < 6) {
			showMessage("密码不能为空或者少于6位！")
			return
		}
		$(this).html("登录中...").addClass("disabled")
		if ($applicationCache.isOnLine()) {
			$store.login(data, callback)
		} else {
			offlineLogin(data, $(this))
		}
	}

	//忘记密码提示事件
	var onFoundPwdTip = function () {
		showMessage("请联系护尔爱项目实施人员重置密码！")
	}

	/**
	 * 初始化登录页面
	 * 入参：
	 * 	params.domId = 登录页面登录框DOM ID
	 * 登录框内元素Class命名规则：
	 * 	.bb-username 	= 账号
	 *  .bb-password 	= 密码
	 *  .bb-submit 	 	= 登录提交
	 *  .bb-reset 	 	= 重置表单
	 *  .bb-message  	= 错误提示信息
	 *  .bb-remember-me = 记住登录状态
	 *  .bb-found-pwd	= 找回密码
	 */
	login.init = function (params) {
		defaults = {
			domId: params.domId
		}
		if ($bb.getIocalStorage("data")) var data = JSON.parse($bb.getIocalStorage("data"))
		if (data) {
			$("#" + defaults.domId + " .bb-username").val(data.username)
			$("#" + defaults.domId + " .bb-password").val(data.password)
			if (!data.remember) $("#" + defaults.domId + " .bb-remember-me").attr("checked", false)
		}
		$("#" + defaults.domId + " .bb-found-pwd").click(onFoundPwdTip)
		$("#" + defaults.domId + " .bb-submit").click(onSubmit)
		//更新应用缓存
		$applicationCache.updateCache()
	}

	return login
})
